package com.heking.security.sensitive;

/**
 * 脱敏工具
 *
 * @Author 35574
 * @Date 2021/10/29
 * @Description
 */
public class SensitiveUtils {
    private final static int NEW_CARD_NUMBER_LENGTH = 18;
    private final static int OLD_CARD_NUMBER_LENGTH = 15;

    /**
     * 加密email显示
     *
     * @param email
     * @return eg: zhang****@qq.com
     */
    public static String email(String email) {
        if (email == null || email.length() < 2) {
            //长度<2或为空
            return email;
        }
        if (email.contains("@")) {
            int index = email.lastIndexOf("@");
            String result = "";
            String suffix = email.substring(index);
            //
            if (index == 0) {
                //第一个就是@
                return email;
            } else if (index == 1) {
                //x@xx
                return "*" + suffix;
            } else {
                //xxx@xx
                int start = index / 2;
                result = email.substring(0, start) + getXing(index - start) + suffix;
            }
            return result;
        } else {
            //后面的一半加密
            int startIndex = email.length() >> 1;
            String result = email.substring(0, startIndex) + getXing(email.length() - startIndex);
            return result;
        }
    }

    /**
     * 创建*
     *
     * @param count *的个数
     * @return
     */
    private static String getXing(int count) {
        String xing = "";
        for (int i = 0; i < count; i++) {
            xing += "*";
        }
        return xing;
    }

    /**
     * 隐藏身份证号码中间
     *
     * @param idCard
     * @return
     */
    public static String idCardNumber(String idCard) {
        if (idCard == null) {
            return null;
        }
        //在replace函数中，$1,$2,$3，可以分别用来表示：
        //正则表达式中，匹配出的第1、2、3个子表达式。
        //这里 $1表示匹配到的(\d{6})
        //这里 $2表示匹配到的(\w{4})
        if (idCard.length() == NEW_CARD_NUMBER_LENGTH) {
            return idCard.replaceAll("(\\d{6})\\d{8}(\\w{4})", "$1*****$2");

        } else if (idCard.length() == OLD_CARD_NUMBER_LENGTH) {
            return idCard.replaceAll("(\\d{6})\\d{6}(\\d{3})", "$1*****$2");
        }
        return idCard;
    }

    /**
     * 隐藏电话号码的中间部分
     *
     * @param phone
     * @return
     */
    public static String phone(String phone) {
        if (phone == null) {
            return null;
        }
        if (phone.length() == 11) {
            return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        } else if (phone.length() >= 4) {
            String s = phone.substring(0, 2);
            for (int i = 0; i < phone.length() - 4; i++) {
                s += "*";
            }
            s += phone.substring(phone.length() - 2);
            return s;
//            return phone.replaceAll("(\\d{2})\\d{0,100}(\\d{2})","$1*****$2");
        }
        return phone;
    }

    /**
     * 只显示前N个,其余的用*代替
     *
     * @param name
     * @param showNumber 显示的数目
     * @return
     */
    public static String showFront(String name, int showNumber) {
        if (name.length() > showNumber) {
            StringBuilder builder = new StringBuilder();
            builder.append(name, 0, showNumber);
            for (int i = showNumber; i < name.length(); i++) {
                builder.append("*");
            }
            return builder.toString();
        } else {
            return name;
        }
    }

    /**
     * 只显示后N个,其余的用*代替
     *
     * @param name
     * @param showNumber 显示的数目
     * @return
     */
    public static String showAfter(String name, int showNumber) {
        if (name.length() > showNumber) {
            StringBuilder builder = new StringBuilder();
            int hide = name.length() - showNumber;
            for (int i = 0; i < hide; i++) {
                builder.append("*");
            }
            builder.append(name, showNumber, name.length());
            return builder.toString();
        } else {
            return name;
        }
    }



}
